package com.funny.learning.english.activity;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.BaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.funny.learning.english.api.TiengAnhApi;
import com.funny.learning.english.dialog.ListOptionDialog;
import com.funny.learning.english.model.CourseEntity;
import com.funny.learning.english.model.LanguageEntity;
import com.funny.learning.english.util.PreferenceShUtil;

public class OptionActivity extends BaseActivity {
	
	private String TAG = "OptionActivity";
	
	private ListOptionDialog dialogListLang;
	private ListOptionDialog dialogListCourse, dialogListCourse1;
	private LayoutInflater inflater;
	private View vListLang, vListCourse, vPayment;
	
	private String langReg = "";
	
	private int[] lever;
	private String idCourse;
	private String hl;
	private String type;
	
	private boolean isCheckPayment = false;
	
	private ArrayList<CourseEntity> arrCourseMain = new ArrayList<CourseEntity>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.option_layout);
		inflater = LayoutInflater.from(activity);
		
		initDialogLang();
	}

	private void initDialogLang(){
		vListLang = inflater.inflate(R.layout.list_dialog, null);
		final LinearLayout llList = (LinearLayout)vListLang.findViewById(R.id.llList);
		Button btChon = (Button)vListLang.findViewById(R.id.btChon);
		btChon.setVisibility(View.GONE);
		if(appication.getLanguageEntities() == null || appication.getLanguageEntities().size() == 0){
			TiengAnhApi.getLanguageList(this, new IAjaxCallbackMMK() {

				@Override
				public <T> void handler(T t, int code) {
					if (code == COMPLETE) {
//						StringBuilder stringBuilder = new StringBuilder();
						ArrayList<LanguageEntity> languageEntities = (ArrayList<LanguageEntity>) t;
						
						appication.setLanguageEntities(languageEntities);
						
						for (int i = 0; i < languageEntities.size(); i++) {
							final LanguageEntity languageEntity = languageEntities.get(i);
							View view = inflater.inflate(R.layout.item_list_dialog, null);
							TextView tvItem = (TextView)view.findViewById(R.id.tvItem);
							ImageView ivCheck = (ImageView)view.findViewById(R.id.ivCheck);
							ivCheck.setVisibility(View.GONE);
							tvItem.setText(languageEntity.getName());
							
							tvItem.setOnClickListener(new OnClickListener() {
								
								@Override
								public void onClick(View arg0) {
									
									appication.setLanguageEntity(languageEntity);
									
									langReg = languageEntity.getHl();
									dialogListLang.dismiss();
									TiengAnhApi.registerLanguage(langReg, appication.getUserEntity(), activity,
											mAjaxCallbackMMK2);
								}
							});
							
							llList.addView(view);
						}
						
//						PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_LIST_LANG, stringBuilder.toString());
						dialogListLang = new ListOptionDialog(activity, vListLang);
						dialogListLang.setOnCancelListener(new OnCancelListener() {
							
							@Override
							public void onCancel(DialogInterface arg0) {
								onBackPressed();
							}
						});
						dialogListLang.getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.dialog_option_width), LayoutParams.WRAP_CONTENT);
						dialogListLang.show();
//						Toast.makeText(activity, languageEntities.size() + "",
//								2000).show();
					} else if (code == ERROR) {

					}
				}
			});
		}
		
		
	}
	
	private void initDialogListCourse(String lang){
		vListCourse = inflater.inflate(R.layout.list_dialog, null);
//		TextView tvTitle = (TextView)vListCourse.findViewById(R.id.tvTitle);
		final LinearLayout llList = (LinearLayout)vListCourse.findViewById(R.id.llList);
		ImageView ivTitle = (ImageView)vListCourse.findViewById(R.id.ivTitle);
		
		
		ivTitle.setImageResource(R.drawable.ic_title_list_course_dialog);
		if(appication.getArrCourse() == null || appication.getArrCourse().size() <= 0){
			TiengAnhApi.getCourseList(this,appication.getUserEntity(), lang, new IAjaxCallbackMMK() {

				@Override
				public <T> void handler(T t, int code) {
					if (code == COMPLETE) {
						arrCourseMain = new ArrayList<CourseEntity>();
						arrCourseMain = (ArrayList<CourseEntity>) t;
						
						appication.setArrCourse(arrCourseMain);
						addDataCourse(llList);
						
						dialogListCourse = new ListOptionDialog(activity, vListCourse);
						dialogListCourse.getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.dialog_option_width), LayoutParams.WRAP_CONTENT);
						dialogListCourse.show();
					} else if (code == ERROR) {

					}
				}
			});
		}else{
			arrCourseMain = new ArrayList<CourseEntity>();
			arrCourseMain = appication.getArrCourse();
			addDataCourse(llList);
			
			dialogListCourse = new ListOptionDialog(activity, vListCourse);
			dialogListCourse.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface arg0) {
					onBackPressed();
				}
			});
			dialogListCourse.getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.dialog_option_width), LayoutParams.WRAP_CONTENT);
			dialogListCourse.show();
		}
		
		
//		TiengAnhApi.getLanguageList(this, new IAjaxCallbackMMK() {
//
//			@Override
//			public <T> void handler(T t, int code) {
//				if (code == COMPLETE) {
//					ArrayList<LanguageEntity> languageEntities = (ArrayList<LanguageEntity>) t;
//					for (int i = 0; i < languageEntities.size(); i++) {
//						LanguageEntity languageEntity = languageEntities.get(i);
//					
//						View view = inflater.inflate(R.layout.item_list_dialog, null);
//						TextView tvItem = (TextView)view.findViewById(R.id.tvItem);
//						
//						tvItem.setText(languageEntity.getName());
//						
//						tvItem.setOnClickListener(new OnClickListener() {
//							
//							@Override
//							public void onClick(View arg0) {
//								
//							}
//						});
//						
//						llList.addView(view);
//					}
//					dialogListLang = new ListOptionDialog(activity, vListLang);
//					dialogListLang.getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.dialog_option_width), LayoutParams.WRAP_CONTENT);
//					dialogListLang.show();
////					Toast.makeText(activity, languageEntities.size() + "",
////							2000).show();
//				} else if (code == ERROR) {
//
//				}
//			}
//		});
	}
	
	private IAjaxCallbackMMK mAjaxCallbackMMK2 = new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {
			// TODO Auto-generated method stub
			if (code == COMPLETE) {
				Toast.makeText(activity, "OK", 2000).show();
			} else if (code == ERROR) {
				Toast.makeText(activity, (String)t, 2000).show();
			}
			PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_VALUE_REG_LANG, langReg);
			initDialogListCourse(langReg);
		}
	};
	
	private IAjaxCallbackMMK mAjaxCallbackRegCourse= new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {
			// TODO Auto-generated method stub
			if (code == COMPLETE) {
				
			} else if (code == ERROR) {
				Toast.makeText(activity, (String)t, 2000).show();
			}
			Intent intent = new Intent(activity, HomeActivity.class);
			startActivity(intent);
			finish();
		}
	};
	
	private void initDialogPayment(){
		vListCourse = inflater.inflate(R.layout.list_dialog, null);
		ImageView ivTitle = (ImageView)vListCourse.findViewById(R.id.ivTitle);
		Button btChon= (Button)vListCourse.findViewById(R.id.btChon);
		
		ivTitle.setImageResource(R.drawable.ic_title_list_payment_dialog);
		final LinearLayout llList = (LinearLayout)vListCourse.findViewById(R.id.llList);
		
		View viewItem1 = inflater.inflate(R.layout.item_list_dialog, null);
		final TextView tvItem = (TextView)viewItem1.findViewById(R.id.tvItem);
		final ImageView ivCheck = (ImageView)viewItem1.findViewById(R.id.ivCheck);
		ivCheck.setVisibility(View.GONE);
		RelativeLayout rlItem = (RelativeLayout)viewItem1.findViewById(R.id.rlItem);
		tvItem.setText("Thanh toán");
		tvItem.setTag("payment");
		
		View viewItem2 = inflater.inflate(R.layout.item_list_dialog, null);
		final TextView tvItem2 = (TextView)viewItem2.findViewById(R.id.tvItem);
		final ImageView ivCheck2 = (ImageView)viewItem2.findViewById(R.id.ivCheck);
		ivCheck2.setVisibility(View.GONE);
		RelativeLayout rlItem2 = (RelativeLayout)viewItem2.findViewById(R.id.rlItem);
		tvItem2.setText("Dùng thử");
		tvItem2.setTag("trial");
		
		rlItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				type = (String) tvItem.getTag();
				appication.setTypePayment(type);
				
//				ivCheck2.setImageResource(R.drawable.ic_uncheck);
//				ivCheck.setImageResource(R.drawable.ic_checked);
				
				TiengAnhApi.registerCourse(idCourse, hl, type, lever, activity, mAjaxCallbackRegCourse);
			}
		});
		rlItem2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				type = (String) tvItem2.getTag();
				appication.setTypePayment(type);
				
//				ivCheck2.setImageResource(R.drawable.ic_checked);
//				ivCheck.setImageResource(R.drawable.ic_uncheck);
				
				TiengAnhApi.registerCourse(idCourse, hl, type, lever, activity, mAjaxCallbackRegCourse);
			}
		});
		llList.addView(viewItem1);
		llList.addView(viewItem2);
		
		btChon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TiengAnhApi.registerCourse(idCourse, hl, type, lever, activity, mAjaxCallbackRegCourse);
			}
		});
		
		dialogListCourse1 = new ListOptionDialog(activity, vListCourse);
		dialogListCourse1.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface arg0) {
				onBackPressed();
			}
		});
		dialogListCourse1.getWindow().setLayout(getResources().getDimensionPixelSize(R.dimen.dialog_option_width), LayoutParams.WRAP_CONTENT);
		dialogListCourse1.show();
		
	}
	
	private void addLevel(LinearLayout linearLayout, int count, int pos){
		final CourseEntity courseEntity = arrCourseMain.get(pos);
		for (int j = 0; j < count; j++) {
			View viewItem = inflater.inflate(R.layout.item_list_course_dialog, null);
			final TextView tvItem = (TextView)viewItem.findViewById(R.id.tvItem);
			if(count == 3){
				if(j == 0){
					tvItem.setTag(1);
					tvItem.setText(courseEntity.getLevel1().getName());
				}else if (j == 1) {
					tvItem.setTag(2);
					tvItem.setText(courseEntity.getLevel2().getName());
				}else if (j == 2) {
					tvItem.setTag(3);
					tvItem.setText(courseEntity.getLevel3().getName());
				}
			}else if(count == 2){
				if(j == 0){
					tvItem.setTag(1);
					tvItem.setText(courseEntity.getLevel1().getName());
				}else if (j == 1) {
					tvItem.setTag(2);
					tvItem.setText(courseEntity.getLevel2().getName());
				}
			}else{
				tvItem.setTag(1);
				tvItem.setText(courseEntity.getLevel1().getName());
			}
			tvItem.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					int leverSelect = (Integer) tvItem.getTag();
//					Log.d(TAG, "leverSelect::" + leverSelect);
//					
					int[] leverTag = {leverSelect};
//					
					lever = leverTag;
					hl = courseEntity.getHl();
					idCourse = courseEntity.getId();
					dialogListCourse.dismiss();
					initDialogPayment();
					appication.setCourseEntity(courseEntity);
					int level = (Integer) tvItem.getTag();
					appication.setLevelSelect(level);
				}
			});
			linearLayout.addView(viewItem);
		}
	}
	
	private void addDataCourse(LinearLayout linearLayout){
		for (int i = 0; i < arrCourseMain.size(); i++) {
			final CourseEntity courseEntity = arrCourseMain.get(i);
		
			View view = inflater.inflate(R.layout.list_course_dialog, null);
			
			View viewHeader = inflater.inflate(R.layout.item_header_list_dialog, null);
			TextView tvHeaderItem = (TextView)viewHeader.findViewById(R.id.tvHeaderItem);
			
			tvHeaderItem.setText(courseEntity.getName());
			
			LinearLayout llListCourse = (LinearLayout)view.findViewById(R.id.llListCourse);
			if(courseEntity.getType().equals("onelevel")){
				addLevel(llListCourse, 1, i);
			}else if(courseEntity.getType().equals("twolevel")){
				addLevel(llListCourse, 2, i);
			}else if(courseEntity.getType().equals("threelevel")){
				addLevel(llListCourse, 3, i);
			}
			linearLayout.addView(viewHeader);
			linearLayout.addView(view);
		}
	}
}
